package com.pomelo.kill.controller;

import com.pomelo.kill.common.AjaxResult;
import com.pomelo.kill.model.Order;
import com.pomelo.kill.service.OrderService;
import com.pomelo.kill.service.RedisService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private RedisService redisService;

    @Resource
    private OrderService orderService;

    /**
     * 查询用户名下所有订单
     */
    @RequestMapping("/all")
    public AjaxResult all(Integer uid) {
        // 已支付的，未支付的
        List<Order> paidList = orderService.selectByUid(uid);
        List<Order> obligation = redisService.obligation(uid);
        List<Order> orders = new ArrayList<>();
        if (paidList != null && paidList.size() != 0) {
            orders.addAll(paidList);
        }
        if (obligation != null && obligation.size() != 0) {
            orders.addAll(obligation);
            return AjaxResult.success(orders);
        }
        return AjaxResult.success(orders);

    }

    /**
     * 根据用户id查询待付款订单
     *
     * @param uid 用户编号
     * @return 待付款订单信息
     */
    @RequestMapping("/obligation")
    public AjaxResult obligation(Integer uid) {
        List<Order> list = redisService.obligation(uid);
        return list == null ? AjaxResult.failure("查询失败") : AjaxResult.success(list);
    }

    /**
     * 根据用户编号查询已付款订单
     *
     * @param uid 用户编号
     */
    @RequestMapping("/paid")
    public AjaxResult paid(Integer uid) {
        List<Order> paidList = orderService.selectByUid(uid);
        return paidList == null ? AjaxResult.failure("查询失败") : AjaxResult.success(paidList);
    }


    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Order order) {
        int i = orderService.updateByPrimaryKeySelective(order);
        return i > 0 ? AjaxResult.success() : AjaxResult.failure("修改失败");
    }
}
